//////////////////////////////////////////////////////////////////////////

#ifndef _CLIENT_H_
#include "client.h"
#endif

//////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <cassert>

//////////////////////////////////////////////////////////////////////////


Client::Client()
{
    AddEventToObserve(Observer::NETWORK_SEND_COMPLETE_TCP);
    AddEventToObserve(Observer::NETWORK_SEND_COMPLETE_UDP);
    AddEventToObserve(Observer::NETWORK_RECEIVE_COMPLETE_TCP);
    AddEventToObserve(Observer::NETWORK_RECEIVE_COMPLETE_UDP);

    m_Conn.AddObserver(this);
}


//////////////////////////////////////////////////////////////////////////


void Client::Connect()
{
    m_Conn.Connect("localhost");
}


//////////////////////////////////////////////////////////////////////////


void Client::Send( const std::string& _str )
{
    Packet buffer( _str.c_str(), _str.length() );
    m_Conn.SendTCP(buffer);
    m_Conn.SendUDP(buffer);
}


//////////////////////////////////////////////////////////////////////////


void Client::OnEvent( Event _event, Observable* _observable )
{
    switch (_event)
    {
    case Observer::NETWORK_SEND_COMPLETE_TCP:
        {
            std::cout << "TCP: Sending data from client to server completed" << std::endl;
            break;
        }

    case Observer::NETWORK_SEND_COMPLETE_UDP:
        {
            std::cout << "UDP: Sending data from client to server completed" << std::endl;
            break;
        }

    case Observer::NETWORK_RECEIVE_COMPLETE_TCP:
        {
            Packet buffer = m_Conn.GetRecvDataTCP();
            std::string str( buffer.Data(), buffer.Size() );
            std::cout << "TCP: Response from server: " << str << std::endl;
            break;
        }

    case Observer::NETWORK_RECEIVE_COMPLETE_UDP:
        {
            Packet buffer = m_Conn.GetRecvDataUDP();
            std::string str( buffer.Data(), buffer.Size() );
            std::cout << "UDP: Response from server: " << str << std::endl;
            break;
        }

    default:
        assert(0);
        break;
    }
}


//////////////////////////////////////////////////////////////////////////


void Client::Close()
{
    m_Conn.Close();
}


//////////////////////////////////////////////////////////////////////////
